package org.feiye.studyday28;

import java.io.*;
import java.net.*;

public class Clientside3 {
	public static void main(String[] args) {
		Socket s = null;
		OutputStream o = null;
		try {
			s = new Socket("192.168.10.93", 6666);
			InputStream is = s.getInputStream();
			// 读文件名长度
			int length = is.read();
			// 读文件名并转换成字符串
			byte[] b = new byte[length];
			int nl = is.read(b);
			String name = new String(b, 0, nl);
			System.out.println(name);

			File file = new File("E:\\pic", name);

			// 读图片
//			int len = 0;
			o = new FileOutputStream(file);
//			while ((len = is.read()) > -1) {
//				o.write(len);
//			}
			byte[] bytes = new byte[10240];
			int len = 0;
			while ((len = is.read(bytes)) > -1) {
				o.write(bytes, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (o != null)
				try {
					o.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			if (s != null) {
				try {
					s.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}
	}
}
